package wiki;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.Root;

/**
 * Servlet implementation class GetCommentDetails
 */
@WebServlet("/GetCommentDetails")
public class GetCommentDetails extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetCommentDetails() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();

		//System.out.println("GetCommentDetails");
		Root root = new Root();
		ResultSet rs = null;
		PreparedStatement pst = null;
		String sql1 = "";
		try {

			String commentid = request.getParameter("commentid");

			//System.out.println("Commentid: "+ commentid);
			
			JSONObject comment;

			if ((commentid != null) && (commentid.length() > 0)) {

				// ncommentid = Integer.parseInt(commentid);
				root.initConnection();

				JSONObject comments = new JSONObject();
				sql1 = "Select comment from issuedetails where id= ?";
				pst = root.con.prepareStatement(sql1);
				pst.setString(1, commentid);
				//System.out.println("PST: " + pst);
				rs = pst.executeQuery();
				JSONArray commentlist = new JSONArray();
				while (rs.next()) {
					comment = new JSONObject();
					comment.put("comment", rs.getString("comment"));
					commentlist.add(comment);
				}
				// out.println("{\"issuedetails\": [" + issuelist.toString()
				// + "]}");

				comments.put("comment", commentlist);
				out.println(comments);

			}

		} catch (Exception e) {
			System.out.println("Exception getting comment detail: "
					+ e.toString());
		}finally{
			try {
				pst.close();
				rs.close();
				root.closeConnection();
			} catch (Exception e) {
				
			}
			
		}

		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

}
